import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
        //text01();
        //text02();
        //这个text03会报错，使用submit标签需要点击的元素必须在form中，但是此时
        //点击的是a标签（超链接标签）不再form标签中
        //text03();
        //text04();
        //text05();
        //text06();
        //显示等待和隐式等待的区别：显示等待是等待自定义的条件满足时，隐式等待是等到所有的元素
        //text07();
        //text08();
        //text09();
        //page01();
        //page02();
        // page03();
        //page04();  //针对alert 进行操作
        //page05();
        //text10();
        //text11();
        text12();
    }

    private static void text12() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File("D://20230719jietu.png"));
    }

    private static void text11() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        //通过 getWindowHandle 获取 get 打开的页面窗口句柄
        //通过 getWindowHandles 获取所有的窗口句柄
        Set<String> handles = webDriver.getWindowHandles();
        String target_handle = "";
        for (String handle : handles) {
            target_handle = handle;
        }
        webDriver.switchTo().window(target_handle);
        sleep(2000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
    }

    private static void text10() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(3)")).click();
        sleep(2000);
        //操作关闭浏览器 ：
        // quit（关闭全部的页面），close（在打开新的页面时不会关闭新打开的页面，只会关闭最开始打开的页面）
        //webDriver.quit();
        webDriver.close();
    }

    private static void page05() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("file:///D:/git%20install(2)/java-advanced-learning/_20230719textCode2/src/main/Page/text05.html");
        webDriver.findElement(By.cssSelector("input")).sendKeys("C:\\Users\\liangyuechushi\\Desktop\\相关课件\\3.MySQL");

    }

    private static void page04() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("file:///D:/git%20install(2)/java-advanced-learning/_20230719textCode2/src/main/Page/text04.html");
        webDriver.findElement(By.cssSelector("body > button")).click();
        sleep(2000);
        //alert 弹窗取消
        webDriver.switchTo().alert().dismiss();
        //点击按钮
        webDriver.findElement(By.cssSelector("body > button")).click();
        //在alert弹
        // 窗中输入字符串
        webDriver.switchTo().alert().sendKeys("焦奥");
        //alert弹窗中确认
        sleep(1000);
        webDriver.switchTo().alert().accept();
    }

    private static void page03() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("file:///D:/git%20install(2)/java-advanced-learning/_20230719textCode2/src/main/Page/text03.html");
        WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        Select select = new Select(webElement);
        select.selectByIndex(3);
    }

    private static void page02() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("file:///D:/git%20install(2)/java-advanced-learning/_20230719textCode2/src/main/Page/text02.html");
        webDriver.switchTo().frame("f1");
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

    private static void page01() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("file:///D:/git%20install(2)/java-advanced-learning/_20230719textCode2/src/main/Page/text01.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));
        for (int i = 0; i < webElements.size(); i++) {
            //如果每个元素type值 == checkbox 就进行点击
            //getAttribute 获取页面上的元素属性值，里边的type 是当前元素属性
            if (webElements.get(i).getAttribute("type").equals("checkbox")) {
                webElements.get(i).click();
            }else {
                //否则什么也不操作
            }
        }
    }

    private static void text09() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        // 找到图片按钮
        WebElement webElement = webDriver.findElement(
                By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-pic"));
        //鼠标右击
        Actions actions = new Actions(webDriver);
        sleep(3000);
        //此处最后一定要加上perform，否则是没有效果的
        actions.moveToElement(webElement).contextClick().perform();
    }

    private static void text08() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        //control + A
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
        sleep(3000);
        //control + X
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "X");
        sleep(3000);
        //control + V
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");
    }

    private static void text07() throws InterruptedException {
        //打开百度首页   搜索521
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        //强制等待3秒
        webDriver.findElement(By.cssSelector("#su")).click();
        //浏览器后退
        sleep(3000);
        webDriver.navigate().back();
        sleep(3000);
        //强制等待3秒
        webDriver.navigate().refresh();
        //浏览器前进
        sleep(3000);
        webDriver.navigate().forward();
        sleep(3000);
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        webDriver.manage().window().maximize();
        sleep(3000);
        webDriver.manage().window().fullscreen();
        sleep(3000);
        webDriver.manage().window().setSize(new Dimension(600, 1000));
    }

    private static void text06() {
        //创建一个驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //判断元素是否可以被点击
        WebDriverWait wait = new WebDriverWait(webDriver, 3000);
//        wait.until(ExpectedConditions.elementToBeClickable
//                (By.cssSelector("#bottom_layer > div > p:nth-child(8)")));
          wait.until(ExpectedConditions.titleIs("百度一下，你就知道"));
    }

    private static void text05() {
        ChromeOptions options = new ChromeOptions();
        //允许所有的访问
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        String url = webDriver.getCurrentUrl();
        String title = webDriver.getTitle();
        if (url.equals("https://www.baidu.com/") && title.equals("百度一下，你就知道")){
            System.out.println("当前页面url=：" + url + ", 当前页面title：" + title);
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
    }

    private static void text04() {
        ChromeOptions options = new ChromeOptions();
        //允许所有的访问
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        String button_value = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
//        System.out.println(button_value);
        if (button_value.equals("百度一下")) {
            System.out.println("测试通过");
        }else {
            System.out.println(button_value);
            System.out.println("测试不通过");
        }
    }

    private static void text03() {
        ChromeOptions options = new ChromeOptions();
        //允许所有的访问
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[1]")).submit();
    }

    private static void text02() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        sleep(3000);
        //找到百度搜索输入框，输入”Java07“
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("java107");
        //点击了百度一下按钮
        webDriver.findElement(By.cssSelector("#su")).click();
        //清空百度搜索输入框中的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

    private static void text01() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        // 打开百度的首页
        webDriver.get("https://www.baidu.com");
        //找到百度搜索输入框
        //WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        //输入软件测试
        element.sendKeys("软件测试");
        // 找到百度一下按钮    //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //效验
        //找到搜索结果
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for (int i = 0; i < elements.size(); i++) {
//            System.out.println(elements.get(i).getText());
            //返回的结果有软件测试，证明测试通过，否则软件测试不通过
            if (!elements.get(i).getText().contains("测试") ||
                    !elements.get(i).getText().contains("软件")) {
                System.out.println(elements.get(i).getText());
                System.out.println("测试不通过");
            }else {
                System.out.println("测试通过");
            }
        }
    }
}